<?php

declare(strict_types=1);
/**
 * This file is part of habo_new_media.
 *
 * @link     https://www.berlen.cn/
 * @contact  tech@berlen.cn
 * @license  http://gitlab.rehou.cn/habo/habo_new_media/-/blob/master/README.md
 */
namespace app\command;

use app\enum\ConfigEnum;
use app\enum\KuaiShouEnum;
use app\service\batch\KuaiShouBatchService;
use think\console\Input;
use think\console\Output;
use think\facade\Cache;

class KuaiShouTargetAgePushWork extends BaseWork
{
    protected function configure()
    {
        // 指令配置
        $this->setName('kuai_shou_target_age_push_work');
        $this->setDescription('批量推送快手年龄定向');
    }

    protected function execute(Input $input, Output $output)
    {
        set_time_limit(0);
        ini_set('memory_limit', '-1');
        $output->writeln('=======批量推送快手年龄定向开始' . date('Y-m-d H:i:s', time()) . '=======');
        $st = time() + 300;
        $key = ConfigEnum::CACHE_CONFIG[KuaiShouEnum::BATCH_CONFIG_KEY]['target_age_push'];
        while (time() < $st) {
            $redis = Cache::store('redis')->handler();
            $id = $redis->lPop($key);
            if (! $id) {
                sleep(5);
                continue;
            }
            $output->writeln('=======批量推送快手年龄定向[' . $id . ']开始' . date('Y-m-d H:i:s', time()) . '=======');
            if (KuaiShouBatchService::batchTargetAgePush(intval($id))) {
                $output->writeln('=======批量推送快手年龄定向[' . $id . ']成功' . date('Y-m-d H:i:s', time()) . '=======');
            } else {
                $output->writeln('=======批量推送快手年龄定向[' . $id . ']失败,' . KuaiShouBatchService::getError(0) . date('Y-m-d H:i:s', time()) . '=======');
            }
        }
        $output->writeln('=======批量推送快手年龄定向结束' . date('Y-m-d H:i:s', time()) . '=======');
    }
}
